package com.css.fxfzypg.modules.baseData.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.entity.CountiesCondition;
import com.css.fxfzypg.modules.baseData.entity.HosPeoEntity;
import com.css.fxfzypg.modules.baseData.service.CountiesConditionService;
import com.css.fxfzypg.modules.baseData.queryParams.CountiesConditionQueryParams;
import com.css.fxfzypg.modules.baseData.vo.CountiesConditionVO;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 区县概况信息
 */
@RestController
@RequestMapping("/fxfzypg/baseData")
@Slf4j
public class CountiesConditionController {

    @Resource
    CountiesConditionService countiesConditionService ;

    /**
     * 新增elevation
     * @param countiesCondition
     * @return
     */
    @PostMapping("/saveCountiesCondition")
    public RestResponse saveCountiesCondition (@RequestBody CountiesCondition countiesCondition){
        RestResponse restResponse = null ;
        try{
            restResponse = countiesConditionService.saveCountiesCondition(countiesCondition);
        }catch (Exception e){
            log.error("新增失败",e);
            restResponse = RestResponse.fail("新增用户失败!");
        }
        return restResponse;
    }

    /**
     * 全查询  模糊查询  地貌特性 气候特性为条件
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllCountiesCondition")
    public RestResponse getAllElevation(HttpServletRequest request, CountiesConditionQueryParams queryParams){

        RestResponse restResponse = null;
        try{
           int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
           int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            restResponse = RestResponse.succeed(countiesConditionService.getAllCountiesCondition(queryParams,curPage,
                    pageSize));
        }catch (Exception e){
            log.error("查询区县概况失败!",e);
            restResponse = RestResponse.fail("查询区县概况失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询CountiesCondition数据
     * @param id
     * @return
     */
    @GetMapping("/getCountiesCondition")
    public RestResponse<CountiesCondition> getCountiesCondition(@RequestParam("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(countiesConditionService.getCountiesConditionById(id));
        }catch (Exception e){
            log.error("获取区县概况失败!",e);
            restResponse = RestResponse.fail("获取区县概况失败!");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     * @param id
     * @return
     */
    @PostMapping("/delCountiesCondition")
    public RestResponse deleteCountiesCondition(@RequestParam("id") String id) {
        RestResponse restResponse = null;
        try{
            countiesConditionService.deleteCountiesConditions(id);
            restResponse = ResponseCreator.createSuccessResponse(null,"删除区县概况成功！");
        }catch (Exception e){
            log.error("删除区县概况失败!",e);
            restResponse = RestResponse.fail("删除区县概况失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param countiesCondition
     * @return
     */
    @PostMapping("/updateCountiesCondition")
    public RestResponse updateElevation(@RequestBody CountiesCondition countiesCondition)  {
        RestResponse restResponse = null;
        try{
            restResponse = countiesConditionService.updateCountiesCondition(countiesCondition);
        }catch (Exception e){
            log.error("修改区县概况失败!",e);
            restResponse = RestResponse.fail("修改区县概况失败!");
        }
        return restResponse;
    }

    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcelCountiesCondition")
    public void exportExcel(HttpServletResponse response ) throws IOException {
        try{
            log.error("功能暂时缺失");
            // List<CountiesCondition> list  = countiesConditionService.getAll();
            // FileUtil.exportExcel(list, "区县概况数据", "sheet", CountiesCondition.class, "CountiesCondition", response);
        }catch (Exception e){
            log.error("导出区县概况失败",e);
        }

    }

    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelCountiesCondition")
    public RestResponse importExcel(MultipartFile file) throws IOException {
//             countiesConditionService.importExcel(file);
        try {
            return countiesConditionService.excelImport(file);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail("导入数据失败");
        }
    }

    @PostMapping("/coverConditions")
    public RestResponse cover(@RequestBody List<CountiesCondition> conditions){
        RestResponse restResponse = null;
        try {
            countiesConditionService.cover(conditions);
            restResponse=RestResponse.succeed("成功导入"+conditions.size()+"条.");
        } catch (Exception e) {
            log.error("覆盖区县概况失败",e);
            restResponse =   RestResponse.fail("覆盖区县概况失败");
        }
        return restResponse;
    }
}
